#include "scene.h"
#include "ui_scene.h"
#include "mapmanager.h"
#include "gamemanager.h"
#include <QKeyEvent>
#include <QPainter>
#include <QMouseEvent>
#include <QElapsedTimer>
#include <QDebug>
#include <QTimer>
Scene::Scene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Scene)
{
    ui->setupUi(this);

    camera.updateWindowSize(size());
    camera.updateBackgroundSize(MapManager::instance().background().size());
    camera.toCenter();

    initTimer();
}

Scene::~Scene()
{
    delete ui;
}

void Scene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    camera.setCamera(painter);
    MapManager::instance().paintMap(painter);
    GameManager::instance().paintUnits(painter);
}

void Scene::resizeEvent(QResizeEvent *)
{
    camera.updateWindowSize(size());
}

void Scene::keyPressEvent(QKeyEvent *event)
{
    int key = event->key();
    if(key == Qt::Key_W){
        camera.updatePosition({0, -10});
    } else if(key == Qt::Key_S){
        camera.updatePosition({0, 10});
    } else if(key == Qt::Key_A){
        camera.updatePosition({-10, 0});
    } else if(key == Qt::Key_D){
        camera.updatePosition({10, 0});
    }
}

void Scene::mousePressEvent(QMouseEvent *event)
{
    if(!_mousepressed && event->button() == Qt::LeftButton){
        _mousepressed = true;
        qDebug() << "mouse clicked at : " << event->pos();
        _oldmousepos = event->pos();
    }
    QWidget::mousePressEvent(event);
}

void Scene::mouseMoveEvent(QMouseEvent *event)
{
    if(_mousepressed){
        // event->pos() - _oldmousepos 就是需要调整的距离
        camera.updatePosition(_oldmousepos - event->pos());
        _oldmousepos = event->pos();
    }
    QWidget::mouseMoveEvent(event);
}

void Scene::mouseReleaseEvent(QMouseEvent *event)
{
    _mousepressed = false;
    QWidget::mouseReleaseEvent(event);
}

void Scene::wheelEvent(QWheelEvent *event)
{
    _d_wheel = event->angleDelta().y();
    if(!_timer_wheel->isActive()) _timer_wheel->start(50);
}

void Scene::initTimer()
{
    _timer_update = new QTimer(this);
    connect(_timer_update, &QTimer::timeout, this, [=]{
        update();

        static QElapsedTimer eTimer;
        static bool first = true;

        if (first) {
            eTimer.start();
            first = false;
            return; // 第一帧跳过
        }

        // 使用真实经过的时间作为时间步长
        qint64 dt_ms_real = eTimer.restart();
        static int count = 0;
        count++; // 实际执行次数
        static qint64 passedtime = 0;
        passedtime += dt_ms_real;
        if (passedtime >= 1000) {
            double fps = count * 1000.0 / passedtime;
            ui->label_fps->setText("FPS : " + QString::number(fps, 'f', 1));
            count = 0;
            passedtime = 0;
        }
    });
    _timer_update->start(16);

    _timer_wheel = new QTimer(this);
    connect(_timer_wheel, &QTimer::timeout, this, [this]{
        if(_d_wheel > 0)
            camera.updateScale(camera.scale() + 0.05);
        else if(_d_wheel < 0)
            camera.updateScale(camera.scale() - 0.05);
        _d_wheel = 0;
    });
}


void Scene::on_pushButton_clicked()
{
    GameManager::instance().start();
}
